package com.itheima.service.impl;

import com.itheima.mapper.SetmealMobileMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealGroupItemVO;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMobileMapper setmealMobileMapper;

    /**
     * 套餐查询
     * @return
     */
    public List<Setmeal> list() {
        List<Setmeal> setmeal = setmealMobileMapper.list();
        return setmeal;
    }

    /**
     * 根据套餐ID查询详情
     * @param id
     * @return
     */
    public Setmeal getById(Integer id) {
        Setmeal setmeal = setmealMobileMapper.getById(id);
        Integer setmealId = setmeal.getId();
        List<CheckGroup> checkGroups = new ArrayList<>();
        List<Integer> groupId = setmealMobileMapper.getGroup(setmealId);
        for (Integer gId : groupId) {
            CheckGroup group = setmealMobileMapper.getByGroup(gId);
            List<Integer> itemId = setmealMobileMapper.getItemId(gId);
            List<CheckItem> checkItems = new ArrayList<>();
            for (Integer iId : itemId) {
                CheckItem item = setmealMobileMapper.getItem(iId);
                checkItems.add(item);
            }
            group.setCheckItems(checkItems);
            checkGroups.add(group);
        }
        setmeal.setCheckGroups(checkGroups);

        return setmeal;
    }
}
